package com.future.mayi.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.future.baselib.utils.CommonUtils;
import com.future.mayi.R;
import com.future.mayi.activity.GoodsActivity;
import com.future.mayi.model.OldGoodsModel;
import com.future.mayi.model.IndexModel;
import com.future.mayi.utils.PriceUtils;
import com.future.mayi.view.xrecyclerview.XRecyclerView;

import butterknife.BindView;
import butterknife.ButterKnife;


public class IndexAdapter extends BaseRecyclerViewAdapter<OldGoodsModel> {
    private IndexModel indexModel;
    private int height;

    public IndexAdapter(Context context, XRecyclerView xRecyclerView) {
        super(context, null, xRecyclerView);
        height= (int) ((CommonUtils.getScreenSize(context)[0]-CommonUtils.dip2px(mContext,5))/2.0f);
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ItemViewHolder(getView(parent, R.layout.layout_index_goods_item));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {
        if (viewHolder instanceof ItemViewHolder) {
            ItemViewHolder holder = (ItemViewHolder) viewHolder;
            holder.viewLine.setVisibility(position % 2 == 0 ? View.VISIBLE : View.GONE);
            holder.ivImg.getLayoutParams().height = height;
            holder.tvOldMoney.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            holder.tvOldMoney.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
            OldGoodsModel model = getItem(position);
            if (model == null) return;
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    GoodsActivity.startActivity(mContext, getItem(position).getGoods_id());
                }
            });
            loadImg(holder.ivImg, model.getGoods_img());
            setText(holder.tvMoney, PriceUtils.getPriceText(model.getPrice()));

            if (model.getOriginal_cost() > 0) {
                holder.tvOldMoney.setVisibility(View.VISIBLE);
                setText(holder.tvOldMoney, "¥" + PriceUtils.getPriceText(model.getOriginal_cost()));
            } else {
                holder.tvOldMoney.setVisibility(View.GONE);
            }

            holder.tvDrop.setVisibility(model.getIs_sales() == 1 ? View.VISIBLE : View.GONE);
            holder.tvFq.setVisibility(model.getIs_credit() == 1 ? View.VISIBLE : View.GONE);
            setText(holder.tvTitle, TextUtils.isEmpty(model.getQuality_name()) ? "" : ("【" + model.getQuality_name() + "】")+ (TextUtils.isEmpty(model.getBrand_name()) ? "" : model.getBrand_name()) + model.getGoods_title());
        }


    }


    class HeadViewHolder extends RecyclerView.ViewHolder {


        public HeadViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }




    }


    class ItemViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_img)
        ImageView ivImg;
        @BindView(R.id.tv_y)
        TextView tvY;
        @BindView(R.id.tv_money)
        TextView tvMoney;
        @BindView(R.id.tv_old_money)
        TextView tvOldMoney;
        @BindView(R.id.tv_title)
        TextView tvTitle;
        @BindView(R.id.tv_drop)
        ImageView tvDrop;
        @BindView(R.id.tv_fq)
        ImageView tvFq;
        @BindView(R.id.view_line)
        View viewLine;

        public ItemViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }



}
